package cn.niit.controller;

import cn.niit.entity.Attendance;
import cn.niit.entity.Classes;
import cn.niit.entity.Course;
import cn.niit.entity.query.AttendanceQuery;
import cn.niit.service.AttendanceService;
import cn.niit.service.ClassService;
import cn.niit.service.CourseService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;


@Controller
@RequestMapping("/attendance")
public class AttendanceController {
    @Resource
    private AttendanceService attendanceService;
    @Resource
    private CourseService courseService;
    @Resource
    private ClassService classService;

    @GetMapping("/queryAll")
    public String queryAll(Model model, AttendanceQuery attendanceQuery) {
        PageInfo<Attendance> attendancePageInfo = attendanceService.queryAll(attendanceQuery);
        List<Classes> classesList=classService.queryAllClass();
        List<Course> courseList=courseService.queryAllCourseList();

        model.addAttribute("classesList",classesList);
        model.addAttribute("courseList",courseList);
        model.addAttribute("page", attendancePageInfo);
        return "/attendance/attendance";
    }

    @GetMapping("/select")
    public String select(Model model, AttendanceQuery attendanceQuery) {
        PageInfo<Attendance> attendancePageInfo = attendanceService.select(attendanceQuery);
        List<Classes> classesList=classService.queryAllClass();
        List<Course> courseList=courseService.queryAllCourseList();

        model.addAttribute("classesList",classesList);
        model.addAttribute("courseList",courseList);
        model.addAttribute("page", attendancePageInfo);
        return "attendance/attendance";
    }


    @GetMapping("/edit/{id}")
    public String toEdit(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("attendance", attendanceService.queryAttendanceById(id));
        List<Classes> classesList=classService.queryAllClass();
        List<Course> courseList=courseService.queryAllCourseList();

        model.addAttribute("classesList",classesList);
        model.addAttribute("courseList",courseList);
        return "/attendance/editAttendance";
    }

    @PostMapping("/edit")
    public String edit(Attendance attendance) {
        AttendanceQuery attendanceQuery = new AttendanceQuery();
        Integer id = attendance.getId();
        attendanceQuery.setClassCode(attendance.getClassCode());
        PageInfo<Attendance> attendancePageInfo = attendanceService.queryByClassCode(attendanceQuery);
        boolean b;
        if (id != null) {
            b = attendanceService.editAttendance(attendance);
        } else {
            b = attendanceService.addAttendance(attendance);
        }
        return "redirect:/attendance/queryAll";
    }

    @GetMapping("/update")
    public String update(Model model) {
        Attendance attendance = new Attendance();
        model.addAttribute("attendance", attendance);
        List<Classes> classesList=classService.queryAllClass();
        List<Course> courseList=courseService.queryAllCourseList();

        model.addAttribute("classesList",classesList);
        model.addAttribute("courseList",courseList);
        return "/attendance/editAttendance";
    }

    @GetMapping("/delete/{id}")
    public String deleteAttendanceById(@PathVariable("id") Integer id) {
        boolean b = attendanceService.deleteAttendanceById(id);
        return "redirect:/attendance/queryAll";
    }
}